package com.flyme.module.order.provider.controller;


import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.order.client.api.OrderRemoteService;
import com.flyme.module.order.client.entity.Order;
import com.flyme.module.order.provider.service.OrderService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @author flyme
 */
@Slf4j
@Api(tags = "订单管理")
@RestController
public class OrderController extends BaseController<OrderService, Order> implements OrderRemoteService {

    @Override
    public ResultBody list(PagerInfo pagerInfo, Order order) {
        String userName = ObjectUtils.getParameter("userName");
        String mobile = ObjectUtils.getParameter("mobile");
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("o.id,user.nickName,orderTitle,orderEntity,orderNo,orderBody,targetId,orderNo,orderAmount,orderStatus,payStatus,payType,returnDate,returnAmount,payDate," +
            "userId,user.avatar,user.userName,o.company,user.mobile,linkName,isInvoice,o.createTime," +
            "(SELECT count(si.id) FROM sys_invoice si WHERE si.orderId = (Cast(o.id AS CHAR)) AND si.invoiceStatus = 0) invoiceStatus");
        cq.eq("orderEntity", order.getOrderEntity());
        cq.eq("targetId", order.getTargetId());
        cq.eq("o.id", order.getId());
        cq.eq("o.payType", order.getPayType());
        cq.like("o.company", order.getCompany());
        cq.like("linkPhone", order.getLinkPhone());
        cq.like("linkName", order.getLinkName());
        cq.ge("payDate", order.getStartDate());
        cq.like("user.userName", userName);
        cq.like("user.mobile", mobile);
        cq.le("payDate", order.getEndDate());
        if(!order.getIsInvoice().equals(0)){
            cq.eq("isInvoice", order.getIsInvoice());
            /*if(order.getIsInvoice().equals(1)){//已开
                cq.lt("(SELECT count(si.id) FROM sys_invoice si WHERE si.orderId = (Cast( o.id as char)) AND si.invoiceStatus = 0)", 1);
            }
            if(order.getIsInvoice().equals(2)){//待开
                cq.gt("(SELECT count(si.id) FROM sys_invoice si WHERE si.orderId = (Cast( o.id as char)) AND si.invoiceStatus = 0)", 0);
            }*/
        }
        cq.createAlias("user", "user");
        EntityMap.setEnumConvertInterceptor(order);
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }


    @Override
    public ResultBody get(@PathVariable Long id) {
        Order order = bizService.getById(id);
        return ResultBody.success().setData(order);
    }

    @Override
    public ResultBody update(Order order) {
        boolean result = bizService.update(order);
        return ResultBody.success(result);
    }

    @Override
    public ResultBody delByIds(String[] ids) {
        return bizService.delByIds(ids);
    }

    @Override
    public ResultBody returnOrder(Long orderId, BigDecimal returnAmount, String returnDate, String reason) {
        Boolean tag = bizService.returnAmount(orderId, returnAmount, returnDate, reason);
        return ResultBody.success(tag);
    }

}
